package com.pactera.madp.cp.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.pactera.madp.cp.api.entity.WorkEmployeeStatisticEntity;
import com.pactera.madp.cp.api.vo.employee.*;
import com.pactera.madp.cp.service.*;
import com.pactera.madp.cp.service.sync.MaHisDataBoardService;
import com.pactera.madp.cp.service.sync.MaTodayDataBoardService;
import com.pactera.madp.cp.service.sync.MaTotalDataBoardService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 经理视图服务实现类
 *
 * @author huhao
 * @since 2022-05-23 15:17:22
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class ManagerViewServiceImpl implements ManagerViewService {

    private final IWorkEmployeeService employeeService;
    private final MaHisDataBoardService maHisDataBoardService;
    private final MaTodayDataBoardService maTodayDataBoardService;
    private final IWorkContactEmployeeService contactEmployeeService;
    private final IWorkEmployeeStatisticService empStatisticService;
    private final IWorkMsgService workMsgService;
    private final MaTotalDataBoardService maTotalDataBoardService;

    @Override
    public SidebarMarkingDataTendencyVo head(String wxUserId, String date) {
        String today = DateUtil.today();
        if (today.equals(date)) {
            return maTodayDataBoardService.todayData(wxUserId, date);
        }
        return maHisDataBoardService.hisData(wxUserId, date);
    }

    @Override
    public SidebarPanelDataIndexVo index(String wxUserId) {
        Integer employeeId = employeeService.getEmpIdByWxUserId(wxUserId);
        SidebarPanelDataIndexVo vo = new SidebarPanelDataIndexVo();
//        vo.setVisitorYesterdayNumber(maYesterdayDataBoardService.yesterdayVisitor(wxUserId));
        vo.setVisitorYesterdayNumber(maTotalDataBoardService.visitor(wxUserId));

        vo.setVisitorNumber(maTodayDataBoardService.todayVisitor(wxUserId));
//        vo.setContactNumber(workContactEmployeeStaticsService.yesterdayContactTotal(employeeId));
        vo.setContactNumber(contactEmployeeService.contactTotal(employeeId));
        vo.setContactAdd(contactEmployeeService.contactAdd(employeeId));
        return vo;
    }

    @Override
    public ContactDataVo contactData(String wxUserId) {
        Integer employeeId = employeeService.getEmpIdByWxUserId(wxUserId);
        ContactDataVo vo = new ContactDataVo();
        // 客户总数统计
        vo.setContactTotal(contactEmployeeService.contactTotal(employeeId));
        // 行内客户数统计
        vo.setContactInside(contactEmployeeService.contactInside(wxUserId, employeeId));
        // 昨日联系客户数统计
        vo.setContactEffectiveYesterday(empStatisticService.contactEffectiveYesterday(employeeId));
        return vo;
    }

    @Override
    public List<SidebarEmpTendencyVo> tendency(String wxUserId, Integer dateType, Integer dataType) {
        Integer employeeId = employeeService.getEmpIdByWxUserId(wxUserId);
        if (dataType != null && dataType == 1) {
            return empStatisticService.contactAdd(employeeId, dateType);
        } else {
            return contactEmployeeService.contactInside(wxUserId, employeeId, dateType);
        }
    }

    @Override
    public ContactBriefVo contactBriefing(String wxUserId, Integer dateType) {
        Integer employeeId = employeeService.getEmpIdByWxUserId(wxUserId);
        ContactBriefVo vo = new ContactBriefVo();
        Integer avgReplyTime = 0;
        Integer contactAdd = 0;
        Integer contactEffective = 0;
        Integer messageSend = 0;
        Double repliedRate = 0d;
        vo.setAvgReplyTime(0);
        vo.setRepliedRate(String.format("%.1f", 0d));
        DateTime today = DateUtil.date();
        DateTime end = DateUtil.beginOfDay(today);
        DateTime start;
        if (dateType == 1) {
            start = DateUtil.beginOfDay(DateUtil.offsetDay(end, -7));
        } else if (dateType == 2) {
            start = DateUtil.beginOfDay(DateUtil.offsetDay(end, -30));
        } else {
            start = DateUtil.beginOfDay(DateUtil.offsetDay(end, -1));
        }
        List<WorkEmployeeStatisticEntity> entities = empStatisticService.list(
                Wrappers.<WorkEmployeeStatisticEntity>lambdaQuery()
                        .ge(WorkEmployeeStatisticEntity::getSynTime, start)
                        .lt(WorkEmployeeStatisticEntity::getSynTime, end)
                        .eq(WorkEmployeeStatisticEntity::getEmployeeId, employeeService.getEmpIdByWxUserId(wxUserId))
        );
        if (!entities.isEmpty()) {
            Integer days = entities.size();
            for (WorkEmployeeStatisticEntity entity : entities) {
                avgReplyTime += entity.getAvgReplyTime();
                contactAdd += entity.getNewContactCnt();
                messageSend += entity.getMessageCnt();
                repliedRate += entity.getReplyPercentage();
            }
            avgReplyTime /= days;
            repliedRate /= days;
        }
        vo.setMessageSend(messageSend);
        vo.setContactAdd(contactAdd);
        vo.setAvgReplyTime(avgReplyTime);
        repliedRate = repliedRate == 0d ? 0d : repliedRate / 100;
        vo.setRepliedRate(String.format("%.1f", repliedRate));
        vo.setContactEffective(contactEffective);
        vo.setContactInside(contactEmployeeService.contactInside(wxUserId, employeeId, start, end));
        vo.setContactEffective(workMsgService.contactEffective(dateType, wxUserId));
        return vo;
    }

}
